package xcmg.device.service.basic;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import xcmg.device.dao.entity.SendMethodDictionaryDO;
import xcmg.device.dao.mapper.DataDictionaryMapper;
import xcmg.device.dao.mapper.SendMethodDictionaryMapper;
import xcmg.device.infra.ErrorCode;
import xcmg.device.service.vo.basic.DictionaryRelationVO;
import xcmg.device.service.vo.basic.SendMethodDictionaryVO;
import xcmg.device.util.CompanyUtil;
import yb.ecp.fast.feign.FastGenClient;
import yb.ecp.fast.infra.infra.ActionResult;
import yb.ecp.fast.infra.infra.PageCommonVO;


/**
 * 采购方式service类
 * @Author huhuawei
 */
@Service
public class SendMethodDictionaryService {
    @Autowired
    private SendMethodDictionaryMapper sendMethodDictionaryMapper;
    
    @Autowired
    private DataDictionaryMapper dataDictionaryMapper;

    @Autowired
    private FastGenClient fastGenClient;
    
    @Autowired
    private CompanyUtil companyUtil;
    
    /**
     * 查询
     * @param sendMethodDictionaryVO
     * @return
     */
    public List<SendMethodDictionaryDO> list(SendMethodDictionaryVO sendMethodDictionaryVO){
        //设置排序
        if(StringUtils.isEmpty(sendMethodDictionaryVO.getpId())){
            //一级节点默认pid为0
            sendMethodDictionaryVO.setpId("0");
        }
        PageCommonVO result=new PageCommonVO();
        List<SendMethodDictionaryDO> doList=sendMethodDictionaryMapper.list(sendMethodDictionaryVO);
        for(SendMethodDictionaryDO dic: doList){
            SendMethodDictionaryVO record=new SendMethodDictionaryVO();
            record.setpId(dic.getId());
            int count=sendMethodDictionaryMapper.countByPid(record);
            dic.setChildCount(count);
        }
        return doList;
    }
    
    public List<SendMethodDictionaryDO> listSendMethodWithDataDictory(SendMethodDictionaryVO sendMethodDictionaryVO) {
    	List<SendMethodDictionaryDO> doList = list(sendMethodDictionaryVO);
    	if(companyUtil.isYN(sendMethodDictionaryVO.getOrgId())) {
    		return doList;
    	}
    	if(CollectionUtils.isEmpty(doList)) {
    		doList = new ArrayList<SendMethodDictionaryDO>();
    	}
    	DictionaryRelationVO condition = new DictionaryRelationVO();
    	condition.setParentCode("TYFYFS");
    	List<DictionaryRelationVO> dictionaryRelationVOs = dataDictionaryMapper.listAll(condition);
    	if(CollectionUtils.isNotEmpty(dictionaryRelationVOs)) {
    		for(DictionaryRelationVO vo:dictionaryRelationVOs) {
    			SendMethodDictionaryDO dictionaryDO = new SendMethodDictionaryDO();
    			dictionaryDO.setId(vo.getId());
    			dictionaryDO.setCode(vo.getCode());
    			dictionaryDO.setName(vo.getName());
    			doList.add(dictionaryDO);
    		}
    	}
    	return doList;
    }
    
    public List<SendMethodDictionaryDO> listDirectSendMethod() {
    	SendMethodDictionaryVO sendMethodDictionaryVO = new SendMethodDictionaryVO();
    	sendMethodDictionaryVO.setpId("0");
    	sendMethodDictionaryVO.setOrgId(companyUtil.ynOrgId());
    	return list(sendMethodDictionaryVO);
    }
    
    /**
     * 新增
     * @param model
     * @return
     */
    public ErrorCode add(SendMethodDictionaryVO model){
        //根据code orgid 查询当前字典是否存在
        if(sendMethodDictionaryMapper.count(model)>0){
               return ErrorCode.SendDataDictionaryIsExists;
        }
        ActionResult<String> textResult = fastGenClient.textGuid();
        model.setId(textResult.getValue());
        //新增的数据字典默认有效
        model.setValid((short)1);
        //判断结果
        if(sendMethodDictionaryMapper.add(model)==0){
            return ErrorCode.Failure;
        }
        return ErrorCode.Success;
    }

    /**
     * 删除
     * @param ids
     * @return
     */
    public ErrorCode remove(List<String> ids){
        if(sendMethodDictionaryMapper.remove(ids)==0){
            return ErrorCode.Failure;
        }
        //
        return ErrorCode.Success;
    }

    /**
     * 更新
     * @param model
     * @return
     */
    public ErrorCode update(SendMethodDictionaryVO model){
        if(sendMethodDictionaryMapper.count(model)>0){
            return ErrorCode.SendDataDictionaryIsExists;
        }
        if(sendMethodDictionaryMapper.update(model)==0){
            return ErrorCode.Failure;
        }
        return ErrorCode.Success;
    }

    /**
     * 单条记录查询
     * @param id
     * @return
     */
    public SendMethodDictionaryVO item(String id){
        SendMethodDictionaryDO entity=sendMethodDictionaryMapper.item(id);
        if(entity==null){
            return null;
        }
        SendMethodDictionaryVO model=new SendMethodDictionaryVO();
        BeanUtils.copyProperties(entity,model);
        return model;
    }

    public List<SendMethodDictionaryDO> item(String orgId,String pId,String code){
        List<SendMethodDictionaryDO> sendMethodDictionaryDOS = sendMethodDictionaryMapper.itemByPIdAndCode(orgId, pId, code);
        return sendMethodDictionaryDOS;
    }
}
